#include "GeoPlane.h"

GeoPlane::GeoPlane(float mWidth, float mHeight, int mWSegment, int mHSegment) : Geometry(), m_width(mWidth), m_height(mHeight),
																				m_wSegment(mWSegment),
																				m_hSegment(mHSegment)
{
	this->makeData();
}

//
void GeoPlane::makeData()
{
	Vertices vertices;
	Indices indices;
	float w_half = m_width / 2.0;
	float h_half = m_height / 2.0;
	float dx = m_width / m_wSegment;
	float dy = m_height / m_hSegment;
	//计算顶点坐标和纹理坐标
	for (int iy = 0; iy <= m_hSegment; ++iy)
	{
		float vy = iy * dy - h_half;
		float ty = 1 - static_cast<float>(iy) / m_hSegment;
		for (int ix = 0; ix <= m_wSegment; ++ix)
		{
			float vx = ix * dx - w_half;
			float tx = static_cast<float>(ix) / m_wSegment;
			vertices.push_back({{vx, vy, 0.0}, {tx, ty}});
		}
	}
	//计算索引
	for (unsigned int iy = 0; iy < m_hSegment; ++iy)
		for (unsigned int ix = 0; ix < m_wSegment; ++ix)
		{
			unsigned int a = iy * (m_wSegment + 1) + ix;
			unsigned int b = a + 1;
			unsigned int c = (iy + 1) * (m_wSegment + 1) + ix;
			unsigned int d = c + 1;
			indices.push_back(a);
			indices.push_back(c);
			indices.push_back(d);
			indices.push_back(a);
			indices.push_back(b);
			indices.push_back(d);
		}

	setup(vertices, indices);
}